<?php

namespace slinks\extensions\onecms\core\templating\twig;

use slinks\extensions\onecms\core\templating\IResourceManager;

/**
 * Handles adding resources to template output. 
 * NOTE: we dont implement IResourceManager since we do not want to qualifiy ourselves for injection.
 * 
 * @author Navid Mitchell
 * 
 * @Service
 * @Scope(public=false)
 */
class ResourceExtension extends \Twig_Extension {

    private $resourceManager;

    /**
     *
     * @param IResourceManager $resourceManager
     * @Inject
     */
    function __construct(IResourceManager $resourceManager) {
        $this->resourceManager = $resourceManager;
    }

    public function getName() {
        return 'resource';
    }

    /**
     * Returns a list of functions to add to the existing list.
     *
     * @return array An array of functions
     */
    public function getFunctions() {
        return array(
            'addScript' => new \Twig_Function_Method($this, 'addScript'),
            'addStyleSheet' => new \Twig_Function_Method($this, 'addStyleSheet')
        );
    }

    public function addScript($url, $type='text/javascript') {
        $this->resourceManager->addScript($url, $type);
    }

    public function addStyleSheet($url, $type='text/css', $media=null) {
        $this->resourceManager->addStyleSheet($url, $type, $media);
    }

}